#include "mainwindow.h"
#include "ui_mainwindow.h"


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setFixedSize(400,350);
    this->setAutoFillBackground(true);//
    QPalette pa = this->palette();
    pa.setBrush(QPalette::Window,QBrush(QPixmap("5.jpg").scaled(this->size(),Qt::IgnoreAspectRatio,Qt::SmoothTransformation)));
    this->setPalette(pa);
}

MainWindow::~MainWindow()
{
    delete ui;
}
/*void MainWindow::paintEvent(QPaintEvent *e)
{
    QPainter paint(this);
    paint.drawPixmap(0,0,this->width(),this->height(),QPixmap("./5.jpg"));
}*/

void MainWindow::on_pushButton_2_clicked()
{

    QString s = ui->lineEdit->text();
    QString s1 = ui->lineEdit_2->text();
    QFile file("./usr.txt");
    file.open(QIODevice::ReadOnly|QIODevice::Text);
    int flag = 0;
    while(1)
    {
        QByteArray user = file.readLine();
        if(user == NULL) break;
        QString ss = s+" "+s1+"\n";
        //qDebug()<<ss<<QString(user)<<endl;
        if(ss == QString(user))
        {
            flag = 1;
            break;
        }
    }
    file.close();
    if(flag == 1)
    {
        //QMessageBox::about(NULL,s,"登录成功");
        this->hide();
        fr.show();
        connect(&fr,SIGNAL(send_msg(QString)),this,SLOT(recv_msg(QString)));
        connect(this,SIGNAL(send_info(QString)),&fr,SLOT(recv_info(QString)));

    }
    else
    {
        QMessageBox::warning(NULL,"警告","用户名或密码错误");
    }
}

void MainWindow::on_pushButton_clicked()
{
    exit(0);
}

void MainWindow::recv_msg(QString s)
{
    s = "\0";
    this->show();
}

void MainWindow::on_pushButton_3_clicked()
{
    QString s3 = ui->lineEdit->text();
    QString s4 = ui->lineEdit_2->text();
    QString e = s3+" "+s4+"\n";
    QFile file("./usr.txt");
    file.open(QIODevice::ReadWrite|QIODevice::Text);
    QString usr;
    int flag = 0;
    while(1)
    {
        QByteArray user = file.readLine();
        if(user == NULL) break;
        int i = 0;
        while(1)                ///////判断用户名是否已经存在
        {
            if(user[i] == ' ') break;
            usr[i] = user[i];
            i++;
        }
        //qDebug()<<ss<<QString(user)<<endl;
        if(s3 == usr)
        {
            flag = 1;
            break;
        }
    }
    if(flag)
    {
        QMessageBox::warning(NULL,"警告","用户名已存在");
    }
    else if((s3 == NULL ||s4 == NULL))
    {
        ;
    }
    else if(flag == 0)
    {
        file.write(e.toUtf8());
        file.close();
        QMessageBox::about(NULL,s3,"注册成功");
    }


}
    void QWidget::keyPressEvent(QKeyEvent *event)
    {
        if(event->key() == Qt::Key_Enter  || event->key() == Qt::Key_Return)
        {
            this->focusNextChild();
        }
    }
